--[[

--]]


local delay = 10  -- in seconds
local new_timer = ngx.timer.at
local log = ngx.log
local ERR = ngx.ERR
local check
local http = require("resty.http")


local function action()
  log(ERR, "action")
  local httpc = http.new()
  local resp, err = httpc:request_uri("http://www.hao123.com", {
    method = "GET",
    path = "/",
    headers = {
      ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
    }
  })

  if not resp then
    log(ERR, "request error :")
    log(ERR, err)
    return
  end

  --获取状态码
  log(ERR, "========OK==========")
  log(ERR, resp.status)

  --获取响应头
  for k, v in pairs(resp.headers) do
    log(ERR, k)
  end
  --响应体
  log(ERR, resp.body)
  --获取状态码
  httpc:close()
end


check = function(premature)
  log(ERR, "check")
  if not premature then

    action()
    -- do the health check or other routine work
    local ok, err = new_timer(delay, check)
    if not ok then
      log(ERR, "failed to create timer: ", err)
      return
    end
  end
end

new_timer(delay, function () check(false) end)
